package cn.audit.controller.leader;

import cn.audit.bean.dto.UserCorrelation;
import cn.audit.bean.pojo.Approval;
import cn.audit.bean.pojo.AuditPlan;
import cn.audit.bean.pojo.Project1;
import cn.audit.bean.pojo.User;
import cn.audit.service.approval.ApprovalService;
import cn.audit.service.operation.AuditPlanService;
import cn.audit.service.operation.ProjectsItemService;
import cn.audit.utils.PagerUtil;
import cn.audit.utils.Paging;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 李忆枫
 * @Date: 2019/5/27 10:28
 */
@Controller
@RequestMapping("/Leader")
public class LeaderController {

    @Resource
    private ProjectsItemService projectsItemService;

    @Resource
    private ApprovalService approvalService;

    @Resource
    private AuditPlanService auditPlanService;

    @RequestMapping("/LeaderPlan")
    public String toLeaderPlan(Model model,
                               @RequestParam(value = "page", required = false) Integer page) {

        int page1 = 1;
        if (page != null) {
            page1 = page;
        }

        List<UserCorrelation> progress = projectsItemService.findProjectProgress();
        PagerUtil pu = Paging.findBook(page1, progress);
        model.addAttribute("progress", pu);
        model.addAttribute("pager", page1);

        return "Leader/LeaderPlan";
    }

    @RequestMapping("/HumanResources")
    public String HumanResources(Model model,
                                 @RequestParam(value = "page", required = false) Integer page) {
        int page1 = 1;
        if (page != null) {
            page1 = page;
        }
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pages = PageRequest.of(page1 - 1, 3, sort);
        Page<UserCorrelation> articleDatas = projectsItemService.findProject2(pages);
        //总页面
        model.addAttribute("pageCount", articleDatas.getTotalPages());

        List<UserCorrelation> project2 = articleDatas.getContent();
        model.addAttribute("project2", project2);
        //当前页数
        model.addAttribute("pager", page1);

        return "Leader/HumanResources";
    }

    /**
     * 查詢
     *
     * @param elect
     * @return
     */
    @ResponseBody
    @RequestMapping("/AjaxFindBacklog")
    public Map<String, Object> AjaxFindBacklog(Integer elect) {
        //创建一个map，用来封装数据
        Map adminMap = new HashMap(16);
        List<UserCorrelation> correlations = null;
        if (elect == 1) {
            correlations = auditPlanService.findPlanStatus();
        } else if (elect == 2) {
            correlations = projectsItemService.findBacklog();
        } else if (elect == 3) {

        } else if (elect == 4) {

        }
        adminMap.put("correlations", correlations);
        adminMap.put("elect", elect);

        return adminMap;
    }


    /**
     * 根据id查找
     *
     * @param elect
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/AjaxApprovalById")
    public Map<String, Object> AjaxApprovalById(Integer elect, Integer id) {
        System.out.println("-------------------------------------------------");
        System.out.println(elect + "========");
        //创建一个map，用来封装数据
        Map adminMap = new HashMap(16);
        if (elect == 1) {
            AuditPlan planById = auditPlanService.findAuditPlanById(Long.valueOf(id));
            adminMap.put("byId", planById);
        } else if (elect == 2) {
            Project1 project1ById = projectsItemService.findProject1ById(Long.valueOf(id));
            adminMap.put("byId", project1ById);
        } else if (elect == 3) {

        } else if (elect == 4) {

        }
        adminMap.put("elect", elect);
        adminMap.put("id", id);
        return adminMap;
    }

    @ResponseBody
    @RequestMapping("/AjaxApproval")
    public Boolean AjaxApproval(HttpServletRequest request,
                                Integer audit, Integer auditId, String event, Integer operations, String opinion) {

        if (audit == 1) {
            auditPlanService.updateAuditPlanStatus(Long.valueOf(3), Long.valueOf(auditId));
        } else if (audit == 2) {
            projectsItemService.updateProject1Status(Long.valueOf(3), Long.valueOf(auditId));
        } else if (audit == 3) {

        } else if (audit == 4) {

        }

        Approval approval = new Approval();
        approval.setEvent(event);
        approval.setOperations(Long.valueOf(operations));
        approval.setOpinion(opinion);
        approval.setTime(new Date());
        User user = (User) request.getSession().getAttribute("user");
        approval.setHandler(user.getId());
        approvalService.save(approval);

        return true;
    }

}
